/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.bean.dto;

import java.io.Serializable;
import java.util.List;

import com.hyts.infodb.bean.vo.BaseVO;
import com.hyts.infodb.config.SystemConfiguration;
import com.hyts.infodb.error.subclass.ControllerException;

import lombok.Data;
import lombok.experimental.Accessors;

/** 
 * @author LiBo/Alex
 * @since V1.0
 * @version V1.0 
 */
@Data
@Accessors(chain=true)
public class BaseDTO<T> extends BaseVO<T> implements Serializable{

    /**  
     * @fieldName serialVersionUID
     * @fieldType long
     */ 
    private static final long serialVersionUID = 4039557756121755372L;
    
    /**  
     * <p>返回编码</p> 
     * @fieldName code
     * @fieldType int
     */ 
    private int code;
    
    /**  
     * <p>返回信息</p> 
     * @fieldName message
     * @fieldType String
     */ 
    private String message;
    
    /**  
     * @param operationType
     * @param modelName
     * @return
     * @exception
     */ 
    public BaseDTO<T> success(SystemConfiguration.MessageConfiguration operationType,String modelName) {
        setCode(SystemConfiguration.ResultConfiguration.SUCCESS.getCode());
        String messageFlag = SystemConfiguration.ResultConfiguration.SUCCESS.getMessage();
        switch(operationType) {
            case SAVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case REMOVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case LOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case QUERY:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case DOWNLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case UPLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;  
            case LOGIN:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
            case REGISTER:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
        default:
            throw new ControllerException("支不支持此操作！");
        }
        return this;
    }
    
    /**  
     * @return
     * @exception
     */ 
    public BaseDTO<T> success(SystemConfiguration.MessageConfiguration operationType,String modelName,T content) {
        setCode(SystemConfiguration.ResultConfiguration.SUCCESS.getCode());
        String messageFlag = SystemConfiguration.ResultConfiguration.SUCCESS.getMessage();
        switch(operationType) {
            case SAVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case REMOVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case LOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case QUERY:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case DOWNLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case UPLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;  
            case LOGIN:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
            case REGISTER:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
        default:
            throw new ControllerException("支不支持此操作！");
        }
        setContent(content);
        return this;
    }
    
    /**  
     * @return
     * @exception
     */ 
    public BaseDTO<T> success(SystemConfiguration.MessageConfiguration operationType,String modelName,List<T> contents) {
        setCode(SystemConfiguration.ResultConfiguration.SUCCESS.getCode());
        String messageFlag = SystemConfiguration.ResultConfiguration.SUCCESS.getMessage();
        switch(operationType) {
            case SAVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case REMOVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case LOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case QUERY:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case DOWNLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case UPLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;  
        default:
            throw new ControllerException("支不支持此操作！");
        }
        setContents(contents);
        return this;
    }

    /**  
     * @param operationType
     * @return
     * @exception
     */ 
    public BaseDTO<T> success(SystemConfiguration.MessageConfiguration operationType) {
        setCode(SystemConfiguration.ResultConfiguration.SUCCESS.getCode());
        String messageFlag = SystemConfiguration.ResultConfiguration.SUCCESS.getMessage();
        switch(operationType) {
            case SAVE:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case REMOVE:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case LOAD:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case QUERY:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case DOWNLOAD:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case UPLOAD:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break; 
            case LOGIN:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
            case REGISTER:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
        default:
            throw new ControllerException("支不支持此操作！");
        }
        return this;
    }
    
    
    /**  
     * @return
     * @exception
     */ 
    public BaseDTO<T> failure(SystemConfiguration.MessageConfiguration operationType) {
        setCode(SystemConfiguration.ResultConfiguration.FAILURE.getCode());
        String messageFlag = SystemConfiguration.ResultConfiguration.FAILURE.getMessage();
        switch(operationType) {
            case SAVE:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case REMOVE:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case LOAD:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case QUERY:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case DOWNLOAD:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;
            case UPLOAD:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break; 
            case LOGIN:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
            case REGISTER:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
                
        default:
            throw new ControllerException("支不支持此操作！");
        }
        return this;
    }
        
    /**  
     * @return
     * @exception
     */ 
    public BaseDTO<T> failure(SystemConfiguration.MessageConfiguration operationType,String modelName) {
        setCode(SystemConfiguration.ResultConfiguration.FAILURE.getCode());
        String messageFlag = SystemConfiguration.ResultConfiguration.FAILURE.getMessage();
        switch(operationType) {
            case SAVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case REMOVE:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case LOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case QUERY:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case DOWNLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;
            case UPLOAD:
                message = String.format(operationType.getMessage(), modelName,messageFlag);
                break;  
            case LOGIN:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
            case REGISTER:
                message = String.format(operationType.getMessage(), "",messageFlag);
                break;  
        default:
            throw new ControllerException("支不支持此操作！");
        }
        return this;
    }
    
}
